# Author    :Dun_Hz
# Time      :2024/3/17 14:49
# 背包问题
goods = [(100,20),(60,10),(120,30)]
goods.sort(key=lambda x:x[0]/x[1],reverse=True)
def fractional_backpack(goods,w):
    m = [0 for i in range(len(goods))]
    total = 0
    for i, (price,weight) in enumerate(goods):
        if w>=weight:
            m[i] = weight
            total += price
            w -= weight
        else:
            m[i] = w
            total += m[i]/weight*price
            w = 0
            break
    return total, m
print(fractional_backpack(goods,50))